//
//  ViewController.m
//  speed
//
//  Created by 王建华 on 2025/9/27.
//

#import "ViewController.h"
#import "NBLocationUtils.h"
#import "NBLayerUtils.h"
#import "NBSpeedDashboardView.h"
#import "Masonry.h"
#import "WNCompassView.h"

@interface ViewController () <MKMapViewDelegate, NBLocationDelegate>

@property (strong, nonatomic) MKMapView *mapView;
@property (strong, nonatomic) UIView *leftBoardView;
@property (strong, nonatomic) UIView *leftBoardTopView;
@property (strong, nonatomic) UIView *leftBoardLeftView;
@property (nonatomic, strong) NBLocationUtils *locationManaget;

@property (nonatomic, strong) NBSpeedDashboardView *speedDashboard;

@property (nonatomic, strong) WNCompassView *indicatorView; // 添加属性

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.mapView];
    [self.view addSubview:self.indicatorView];
    [self.view addSubview:self.leftBoardView];
    
    if (isPad) {
        [self.leftBoardView addSubview:self.leftBoardTopView];
        [self.leftBoardView addSubview:self.leftBoardLeftView];
        [self.leftBoardTopView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.mas_equalTo(self.leftBoardView);
            make.height.mas_equalTo(self.leftBoardView.mas_height).multipliedBy(0.55);
            make.width.mas_equalTo(self.leftBoardView.mas_width).multipliedBy(1);
        }];
        [self.leftBoardLeftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.mas_equalTo(self.leftBoardView);
            make.width.mas_equalTo(self.leftBoardView.mas_width).multipliedBy(0.55);
            make.height.mas_equalTo(self.leftBoardView.mas_height).multipliedBy(1);
        }];
    }
    
    [self.view addSubview:self.speedDashboard];

    [self.mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view);
        if (isPad) {
            make.width.mas_equalTo(self.view.mas_width).multipliedBy(1);
        }else {
            make.width.mas_equalTo(self.view.mas_width).multipliedBy(1.35);
        }
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(1.45);
    }];
    
    [self.leftBoardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(self.view);
        if (isPad) {
            make.width.height.mas_equalTo(self.view.mas_width).multipliedBy(0.28);
        }else {
            make.bottom.mas_equalTo(self.view);
            make.width.mas_equalTo(self.view.mas_height);
        }
    }];
    
    [self.indicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(self.leftBoardView.mas_centerX);
        make.height.offset(COM_H);
        make.right.mas_equalTo(self.view.mas_right);
    }];
    
    [self.speedDashboard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(self.leftBoardView);
    }];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        if (isPad) {
            /// 添加渐变背景色
            [NBLayerUtils addgradintLayerWithFram:CGRectMake(0, 0,
                                                             self.leftBoardTopView.frame.size.width,
                                                             self.leftBoardTopView.frame.size.height)
                                          andView:self.leftBoardTopView
                                        andColors:@[ [UIColor colorWithWhite:0. alpha:1.],
                                                     [UIColor colorWithWhite:0. alpha:1.],
                                                     [UIColor colorWithWhite:0. alpha:0.75],
                                                     [UIColor colorWithWhite:0. alpha:0.],
                                                     [UIColor colorWithWhite:0. alpha:0.]]
                                     andLocations:@[ @0, @0.55, @0.695, @0.915, @1]];
            [NBLayerUtils addgradintLayerWithFram:CGRectMake(0, 0,
                                                             self.leftBoardLeftView.frame.size.width,
                                                             self.leftBoardLeftView.frame.size.height)
                                          andView:self.leftBoardLeftView
                                        andColors:@[ [UIColor colorWithWhite:0. alpha:1.],
                                                     [UIColor colorWithWhite:0. alpha:1.],
                                                     [UIColor colorWithWhite:0. alpha:0.75],
                                                     [UIColor colorWithWhite:0. alpha:0.],
                                                     [UIColor colorWithWhite:0. alpha:0.]]
                                    andStartPoint:CGPointMake(0.5, 0)
                                      andEndPoint:CGPointMake(0.5, 1)
                                     andLocations:@[ @0, @0.55, @0.695, @0.915, @1]];
        }else {
            /// 添加渐变背景色
            [NBLayerUtils addgradintLayerWithFram:CGRectMake(0, 0,
                                                             self.leftBoardView.frame.size.width,
                                                             self.leftBoardView.frame.size.height)
                                          andView:self.leftBoardView
                                        andColors:@[ [UIColor colorWithWhite:0. alpha:1.],
                                                     [UIColor colorWithWhite:0. alpha:1.],
                                                     [UIColor colorWithWhite:0. alpha:0.75],
                                                     [UIColor colorWithWhite:0. alpha:0.],
                                                     [UIColor colorWithWhite:0. alpha:0.]]
                                     andLocations:@[ @0, @0.55, @0.695, @0.915, @1]];
        }
        [NBLayerUtils addgradintLayerWithFram:CGRectMake(0, 0,
                                                         self.indicatorView.frame.size.width,
                                                         self.indicatorView.frame.size.height)
                                      andView:self.indicatorView
                                    andColors:@[ [UIColor colorWithWhite:0. alpha:1.],
                                                 [UIColor colorWithWhite:0. alpha:1.],
                                                 [UIColor colorWithWhite:0. alpha:0.75],
                                                 [UIColor colorWithWhite:0. alpha:0.]]
                                andStartPoint:CGPointMake(0.5, 0)
                                  andEndPoint:CGPointMake(0.5, 1)
                                 andLocations:@[ @0, @0.55, @0.695, @1]];
    });
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    self.indicatorView.hidden = YES;
    // 视图显示后播放启动动画
    [self.speedDashboard playStartupAnimationWithCompletion:^(BOOL finished) {
        [self.locationManaget startUpdatingLocationAndHeading];
    }];
}

/// 位置朝向 更新
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(CLLocation *)location
       didUpdateHeading:(CLHeading *)newHeading {

    if ( location && newHeading ) {
        //更新速度
        [self.speedDashboard setSpeed:location.speed*3.6
                             animated:YES];
        CLLocationDirection trueHeading = newHeading.trueHeading+self.locationManaget.correctDirection*90;
        // 更新视角
        [NBLocationUtils updataCameraWithMapView:self.mapView
                                        andSpeed:location.speed
                                      andHeading:trueHeading];
        if (self.indicatorView.hidden) {
            self.indicatorView.hidden = NO;
        }
        [self.indicatorView compassViewDidUpdateHeading:trueHeading];
    }
}

- (void)doubleTapAction {
    self.locationManaget.correctDirection ++;
    if (self.locationManaget.correctDirection >= 4) {
        self.locationManaget.correctDirection = 0;
    }
}

- (NBLocationUtils *)locationManaget {
    if (!_locationManaget) {
        _locationManaget = [[NBLocationUtils alloc] init];
        _locationManaget.delegate = self;
    }
    return _locationManaget;
}

- (NBSpeedDashboardView *)speedDashboard {
    if (!_speedDashboard) {
        _speedDashboard = [[NBSpeedDashboardView alloc] init];
        _speedDashboard.userInteractionEnabled = NO;
    }
    return _speedDashboard;
}

- (MKMapView *)mapView {
    if (!_mapView) {
        _mapView = [[MKMapView alloc] init];
        _mapView.showsUserLocation = YES; // 显示用户当前位置
    }
    return _mapView;
}

- (UIView *)leftBoardView {
    if (!_leftBoardView) {
        _leftBoardView = [[UIView alloc] init];
        
        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                    action:@selector(doubleTapAction)];
        doubleTap.numberOfTapsRequired = 2;
        [_leftBoardView addGestureRecognizer:doubleTap];
    }
    return _leftBoardView;
}

- (UIView *)leftBoardTopView {
    if (!_leftBoardTopView) {
        _leftBoardTopView = [[UIView alloc] init];
    }
    return _leftBoardTopView;
}

- (UIView *)leftBoardLeftView {
    if (!_leftBoardLeftView) {
        _leftBoardLeftView = [[UIView alloc] init];
    }
    return _leftBoardLeftView;
}
- (WNCompassView *)indicatorView {
    if (!_indicatorView) {
        _indicatorView = [WNCompassView compassView];
    }
    return _indicatorView;
}

@end
